package com.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.student.entities.OrderItemEntity;
import com.student.service.OrderService;
import com.student.service.UserService;
import com.student.util.DES;
import com.student.util.JsonTools;
import com.student.util.OrderNo;
import com.sun.org.apache.xpath.internal.operations.Or;

public class OrderServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public OrderServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String reqStr = request.getParameter("request");
		if ("order_commit".equals(reqStr)) {
			doCommit(request, response);
		} else if ("order_track".equals(reqStr)) {
			doTrack(request, response);
		}
	}

	private void doTrack(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String userName = request.getParameter("userName");
		int userId = 0;
		try {
			userId = UserService.getUser(userName).getId();
		} catch (SQLException e) {
			out.print("用户名不对");
		}
		if (userId == 0) {
			out.print("你上报的用户名有误");
		}
		String start = request.getParameter("start");
		String count = request.getParameter("count");

		try {
			out.print(JsonTools.createJsonString(OrderService.getUserOrder(
					userId, start, count)));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

	private void doCommit(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String orderString = request.getParameter("order");
		System.out.println("订单数据（解密前）" + orderString);
		String userName = request.getParameter("userName");
		String toAddr = request.getParameter("toAddr");
		String toUser = request.getParameter("toUser");
		String toTime = request.getParameter("toTime");
		PrintWriter out = response.getWriter();
		try {
			// 对订单数据解密
			orderString = DES.decryptDES(orderString, "liwentao");
		} catch (Exception e) {
			System.out.print("订单数据非法，解密失败");
			out.print("ERROR");
			return;
		}
		System.out.println("订单数据（解密后）" + orderString);
		int userId = 0;
		try {
			userId = UserService.getUser(userName).getId();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			// 将数据保存到数据库
			out.print(OrderService.saveOrder(orderString, userId, toAddr,
					toUser, toTime));
		} catch (SQLException e) {
			e.printStackTrace();
			out.print("ERROR");
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
